﻿using Microsoft.AspNetCore.Mvc;
using MyWeb.Business.System;
using MyWeb.Business.System.Impl;
using MyWeb.Common.Helper;
using MyWeb.Extensions.ServiceExtensions.Swagger;
using MyWeb.Model;
using MyWeb.Model.Dto;
using MyWeb.Model.Models;
using MyWeb.Model.User;
using System.Security.Claims;
using static MyWeb.Extensions.ServiceExtensions.Swagger.CustomApiVersion;

namespace MyWeb.Api.Controllers.V1.System
{
    /// <summary>
    /// 部门信息管理
    /// </summary>
    [Produces("application/json")]
    [CustomRoute(ApiVersions.V1)]
    public class DepartmentController : BaseController
    {
        private readonly IDepartmentService _service;
        public readonly SysUserInfo _sysUser;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="departmentService"></param>
        public DepartmentController(IDepartmentService departmentService, SysUserInfo sysUserInfo)
        {
            _service = departmentService;
            _sysUser = sysUserInfo;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pid"></param>
        /// <param name="key"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetPage(int page = 1, long pid = 0, string key = "", int pageSize = 50)
        {
            var user = HttpContext.Request.Headers;

            var userinfo = user["UserInfo"];
            var userName = User.FindFirstValue(ClaimTypes.Name);
            var request = new DepartmentDto
            {
                Pagination = new PaginationModel
                {
                    PageIndex = page,
                    PageSize = pageSize,
                },
                Pid = pid,
                Key = key,
            };
            var data = await _service.GetDepartment(request);
            return Success(data);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="sysUserInfo">用户信息</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> Edit([FromBody] DepartmentDto department)
        {
            if (department.IsNullOrEmpty())
            {
                return Failed("信息不允许为空！");
            }
            if (department.Id.IsNullOrEmpty())
            {
                return Failed("信息不允许为空！");
            }
            
            return await _service.EditInfo(department) ? Success("成功") : Failed("更新失败");
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="sysUserInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Add([FromBody] Department department)
        {
            if (department.Name.IsNullOrEmpty())
            {
                return Failed(msg: "用户名为空！");
            }

            return await _service.AddInfo(department) ? Success("成功") : Failed(msg: "更新失败");
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="uid">用户Id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> Remove(long uid)
        {
            if (uid <= 0L)
            {
                return Failed<object>("Id不存在！");
            }

            return Success(await _service.Remove(uid));
        }

        /// <summary>
        /// 获取部门树
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetDepartmentTree(long pid = 0)
        {

            var data = await _service.GetDepartmentTree(pid);
            return Success(data, "获取成功");
        }
    }
}
